<?php

class Blacklist {

    function Update() {

        function urlfilesize($url) {
            if (substr($url, 0, 4) == 'http') {
                $x = array_change_key_case(get_headers($url, 1), CASE_LOWER);
                $x = $x['content-length'];
            } else {
                $x = @filesize($url);
            }
            return $x;
        }

        if (isset($_GET['Cmp'])) {
            $OFACV[1]['FileLenght'] = $_GET['Cmp'];
        }
        if (urlfilesize($RMTFILE) != $OFACV[1]['FileLenght']) {
            #Desactualizado
            $GLOBALS['MYT']->db_query("UPDATE hd_amlblacklist SET ablDateUpdate = '{$sysTime}' WHERE ablID = 1");
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $RMTFILE);
            $fh = fopen($GLOBALS['DOC'] . "\SEC_OFAC\\" . $fname . '.zip', "w");
            curl_setopt($ch, CURLOPT_FILE, $fh);
            curl_exec($ch);
            curl_close($ch);
        }
    }

    function LoadModel($Model) {
        #Carrega modelo de blacklist
        include($GLOBALS['BIB'] . 'FileTemplates/BlacklistModels/' . $Model . '.php');
    }

    function VerifyBlacklist() {

// SEC:_CHK->OFAC
        if (!isset($SECQR)) {
            $SECQR = $GLOBALS['MYT']->db_array("SELECT FraID, CliNumero, CliNombre, CliDomicilio, CliCiudad FROM Facturas
											INNER JOIN Clientes ON CliNumero = FraCliNumero
											WHERE FraID IN ({$ORPOOL})");
        }
        $TSECChk = $SECQR[0];
        for ($ck = 1; $ck <= $TSECChk; $ck++) {
            $Name = '+' . str_replace(' ', ' +', $SECQR[$ck]['CliNombre']);
            $Addr = '+' . str_replace(' ', ' +', $SECQR[$ck]['CliDomicilio']);
            $City = '+' . str_replace(' ', ' +', $SECQR[$ck]['CliCiudad']);

            $Nt_Event = $GLOBALS['MYT']->db_array("SELECT Ent_num AS ID, SDN_Name AS MatchStr
										FROM ext_ofac_main
											WHERE match(SDN_Name) against ('$Name' IN BOOLEAN MODE)");
            if (!isset($Nt_Event[1])) {
                $Nt_Event = $GLOBALS['MYT']->db_array("SELECT ent_num AS ID, alt_name AS MatchStr
										FROM ext_ofac_altidentity
											WHERE match(alt_name) against ('$Name' IN BOOLEAN MODE)");
            }
            if (!isset($Nt_Event[1])) {
                $Nt_Event = $GLOBALS['MYT']->db_array("SELECT ent_num AS ID, Address AS MatchStr
										FROM ext_ofac_address
											WHERE match(Address) against ('$Addr' IN BOOLEAN MODE)");
            }
            if (isset($Nt_Event[1])) {
                // Create Alert

                $DBVRS = $GLOBALS['MYT']->db_array("SELECT REF FROM ext_ofac_version ORDER BY REF DESC LIMIT 0,1");
                $GLOBALS['MYT']->db_query("INSERT INTO ex_matches
				(TYPE, DB_Version, STR_MATCHED, STR_STORED, INTERNAL_ID, EXTERNAL_ID, sysTime, INVOICE, STATUS, sysUser)
				VALUES
				('Client','{$DBVRS[1]['REF']}','{$Nt_Event[1]['MatchStr']}','{$Name}',{$SECQR[$ck]['CliNumero']},'{$Nt_Event[1]['ID']}','{$sysTime}',{$SECQR[$ck]['FraID']},1,{$_SESSION[$SESSID]})");
                $GLOBALS['MYT']->db_query("UPDATE facturas SET FraStatus = 8 WHERE FraID = {$SECQR[$ck]['FraID']}");
                $Event_Ct = $ck;
            }
        }
    }

    function Match() {
        
    }

}

?>
